package com.tuananh.radnom.gwt.server;

import java.util.ArrayList;

import com.tuananh.radnom.gwt.shared.dto.MyBaseDTO;
import com.tuananh.radnom.gwt.shared.dto.NetConnDTO;
import com.tuananh.radnom.gwt.shared.dto.NetNodeDTO;
import com.tuananh.radnom.gwt.shared.dto.NetInterfaceDTO;
import com.tuananh.radnom.gwt.shared.dto.NetPollerDTO;
import com.tuananh.radnom.gwt.shared.dto.NetServiceDTO;
import com.tuananh.radnom.model.NetConn;
import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.model.netpoller.NetPollerConcrete;
import com.tuananh.radnom.model.netservice.NetService;

public class ModelDtoConverter {
	public static NetNodeDTO netNode2DTO(NetNode node) {
		NetNodeDTO dto = new NetNodeDTO();
		dto.setName(node.getName());
		dto.setKey(node.getName());
		dto.setDisplayName(node.getDisplayName() != null ? node
				.getDisplayName() : node.getName());
		dto.setValue(dto.getDisplayName());
		if (node.getTechInfo() != null && node.getTechInfo().size() > 0)
			dto.setTechInfo(new MyBaseDTO("TechInfo", node.getTechInfo()
					.toString()));
		if (node.getGeoInfo() != null && node.getGeoInfo().size() > 0)
			dto.setGeoInfo(new MyBaseDTO("GeoInfo", node.getGeoInfo()
					.toString()));
		dto.setNetInterfaces(new ArrayList<NetInterfaceDTO>());
		for (NetInterface netInt : node.getNetInterfaces()) {
			dto.getNetInterfaces().add(netInt2DTO(netInt));
		}
		return dto;
	}

	public static NetInterfaceDTO netInt2DTO(NetInterface netInt) {
		NetInterfaceDTO dto = new NetInterfaceDTO();
		dto.setName(netInt.getName());
		dto.setKey(netInt.getName());
		dto.setDisplayName(netInt.getDisplayName() != null ? netInt
				.getDisplayName() : netInt.getName());
		dto.setValue(dto.getName());
		if (netInt.getIpAddress() != null)
			dto.setIpAddress(new MyBaseDTO("IpAddress", netInt.getIpAddress()));
		if (netInt.getMacAddress() != null)
			dto.setMacAddress(new MyBaseDTO("MacAddress", netInt
					.getMacAddress()));

		if (netInt.getNetServices().size() > 0) {
			dto.setNetServices(new ArrayList<NetServiceDTO>());
			for (NetService netSer : netInt.getNetServices()) {
				dto.getNetServices().add(netService2DTO(netSer));
			}
		}
		return dto;
	}

	public static NetServiceDTO netService2DTO(NetService netService) {
		NetServiceDTO dto = new NetServiceDTO();
		dto.setName(netService.getName());
		dto.setKey(netService.getName());
		dto.setValue(netService.getName());
		dto.setDisplayName(netService.getName());

		if (netService.getParams() != null)
			dto.setParams(new MyBaseDTO("Params", netService.getParams()
					.toString()));
		if (netService.getNetPoller() != null) {
			// dto.setNetPoller(netPoller2DTO(netService.getNetPoller()));
		}
		return dto;
	}

	public static NetPollerDTO netPoller2DTO(NetPollerConcrete netPoller) {
		return null;
	}

	public static NetConnDTO netConn2DTO(NetConn netConn) {
		if (validateNetConn(netConn)) {
			NetConnDTO dto = new NetConnDTO();
			dto.setKey(netConn.getName());
			dto.setName(netConn.getName());
			dto.setDisplayName(netConn.getDisplayName() != null ? netConn
					.getDisplayName() : netConn.getName());
			dto.setValue(dto.getDisplayName());

			dto.setChild(netConn.getChild().getName());
			dto.setParent(netConn.getParent().getName());
			return dto;
		}
		return null;
	}

	static boolean validateNetConn(Object ne) {
		// TODO Auto-generated method stub
		NetConn conn = (NetConn) ne;
		// A connection must have both parent and child netelement
		if (conn.getChild() == null || conn.getParent() == null)
			return false;
		// both endpoint of a connection must be NetInterface (Nic)
		if (!(conn.getChild() instanceof NetInterface)
				|| !(conn.getParent() instanceof NetInterface))
			return false;
		// The name of this connection must be the combination of parent and
		// child with "--" between
		if (!conn.getName().equals(
				conn.getParent().getName() + "--" + conn.getChild().getName()))
			return false;
		return true;
	}

	public static NetNode dto2NetNode(NetNodeDTO dto) {
		NetNode node = new NetNode();

		return node;
	}

	public static NetInterface dto2NetInt(NetInterfaceDTO dto) {
		NetInterface netInt = new NetInterface();
		return netInt;
	}

	public static NetService dto2NetService(NetServiceDTO dto) {
		NetService netService = new NetService();
		netService.setName(dto.getName());
		if (dto.getNetPoller() != null)
			netService.setNetPoller(dto2NetPoller(dto.getNetPoller()));
		return netService;
	}

	public static NetPollerConcrete dto2NetPoller(NetPollerDTO dto) {
		NetPollerConcrete netPoller = new NetPollerConcrete();
		return netPoller;
	}
}
